{include file='Public/header' /}
<div class="layui-layout layui-layout-admin">
{include file='Public/head' /}	
{include file='Public/side' /}
<div class="layui-body"> 
{include file='Public/crumbs' /}
<style>
.hide{ display:none;}
</style>
	<!-- 内容主体区域 -->
	<div class="shu-body">
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12 layui-col-xs12">
					<form class="layui-form" method="get" action="">
						<a href="{:url('add')}" class="layui-btn"><i class="layui-icon">&#xe61f;</i> 添加用户</a>
						<div class="layui-inline">
						用户搜索
						</div>
						<div class="layui-input-inline">
							<select name="field" lay-verify="required">
                                    <option {if condition="input('param.field') eq 'user' "}selected{/if} value="user">用户名</option>
                                    <option {if condition="input('param.field') eq 'phone' "}selected{/if} value="phone">电话</option>
                                    <option {if condition="input('param.field') eq 'qq' "}selected{/if} value="qq">QQ</option>
                                    <option {if condition="input('param.field') eq 'email' "}selected{/if} value="email">邮箱</option>		
                            </select>
				  		</div>
						<div class="layui-inline">
						<input type="text" name="keyword" value="{:input('param.keyword')}" class="layui-input" placeholder="输入关键词">
						</div>
						<div class="layui-inline">
						排序：
						</div>
						<div class="layui-inline">
						<select name="order" class="form-control">
                             <option {if condition="input('param.order') eq 'asc' "}selected{/if} value="asc">注册时间升</option>
                             <option {if condition="input('param.order') eq 'desc' "}selected{/if} value="desc">注册时间降</option>
                        </select>
						</div>
						<div class="layui-inline">
						<button class="layui-btn" lay-submit="" lay-filter="formDemo">提交</button>
						</div>
					</form>
					<form class="layui-form" method="post">
						<table lay-even class="layui-table">
							<colgroup>
							<col width="50">
							</colgroup>
							<thead>
								<tr>
									<th> 
									<input type="checkbox" name="checkAll" lay-filter="checkAll">
									</th>
									<th>用户名</th>
									<th>用户组</th>
									<th class="center">性别</th>
									<th class="center">生日</th>
									<th>电话</th>
									<th>Q&nbsp;Q</th>
									<th>邮箱</th>
									<th class="center">操作</th>
								</tr>
							</thead>
							<tbody>
							{volist name="list" id="val"}
							<tr>
								<td align="center"> 
								{if condition="$val['uid'] neq 1"}
									<input type="checkbox" name="ids[{$val['uid']}]" lay-filter="checkOne" value="{$val['uid']}">
								{else/}
									<span title="系统管理员，禁止删除">--</span> 
								{/if}
								</td>
								<td>{$val['user']}</td>
								<td class="grouptd">
									<span class="group" val="{$val['uid']}">{$val['title']}</span>
									<div class="groupselect hide" style="width:120px;">
									{if condition="$val['uid'] eq 1"}
									<select lay-verify="required" lay-filter="test" val="{$val['uid']}">
									<option value="1">超级管理员</option>
									</select>
									{else/}
									<select lay-verify="required" lay-filter="test">
										{volist name="group" id="v"}
											<option
											{if condition="$val['gid'] eq $v['id']"}selected="selected"{/if}
											value="{$v.id}">{$v.title}</option>
										{/volist}
									</select>
									</div>
									{/if}
                                </td>
								<td><?php if($val['sex']==1){echo '男';}elseif($val['sex']==2){echo '女';}else{echo '保密';} ?></td>
								<td>{$val['birthday']|date="Y-m-d",###}</td>
								<td>{$val['phone']}</td>
                                <td>{$val['qq']}</td>
                                <td>{$val['email']}</td>
								<td>
								<a class="layui-btn layui-btn-small" href="{:url('edit',array('uid'=>$val['uid']))}">修改</a>
								{if condition="$val['uid'] neq 1"}
								<a class="layui-btn layui-btn-small layui-btn-danger del_btn"  href="javascript:;" nickname="{$val['user']}" val="{:url('del',array('ids'=>$val['uid']))}">删除</a> 
								{/if} 
								</td>
							</tr>
							{/volist}
							</tbody>
						</table>
						<a class="layui-btn" lay-submit lay-filter="delete"><i class="layui-icon">&#xe640;</i> 批量删除</a>
						<div class="shu-page">
						{$list->render()}
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- 内容主体区域 END--> 
</div>
{include file='Public/footer' /} 
<script>
layui.use('form',function(){
  var form = layui.form,
  jq = layui.jquery;

	jq(".group").click(function () {
		jq(this).addClass('hide');
		jq(this).parent().find(".groupselect").removeClass('hide');
	});
	//监听下拉
	form.on('select(test)', function(data){
		var ob = jq(this);
		var gid = data.value;
		var uid = jq('.group').attr('val');
		loading = layer.load(2, {
			shade: [0.2,'#000']
		});
		console.log(uid);
		jq.get("{:Url('update')}?ajax=yes&uid=" + uid + "&gid=" + gid, function (data) {
			if(data.code == 200){
			  layer.close(loading);
			  layer.msg(data.msg, {icon: 1, time: 1000}, function(){
				location.reload();
			  });
			}else if(data.code == 0){
			  layer.close(loading);
			  layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
			}
		});
	});

  form.on('checkbox(checkAll)', function(data){
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").prop('checked',true);
    }else{
      jq("input[lay-filter='checkOne']").prop('checked',false);
    }
    form.render('checkbox');
  });  

  form.on('checkbox(checkOne)', function(data){
    var is_check = true;
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").each(function(){
        if(!jq(this).prop('checked')){ is_check = false; }
      });
      if(is_check){
        jq("input[lay-filter='checkAll']").prop('checked',true);
      }
    }else{
      jq("input[lay-filter='checkAll']").prop('checked',false);
    } 
    form.render('checkbox');
  });
  
  jq('.del_btn').click(function(){
	var name = jq(this).attr('nickname');
    var url = jq(this).attr('val');
    layer.confirm('确定删除【'+name+'】用户?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      jq.get(url,function(data){
        if(data.code == 200){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
        }
      });
    });   
  });

  form.on('submit(delete)', function(data){
    var is_check = false;
    jq("input[lay-filter='checkOne']").each(function(){
      if(jq(this).prop('checked')){ is_check = true; }
    });
    if(!is_check){
      layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
      return false;
    }
    layer.confirm('确定批量删除?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      var param = data.field;
      jq.post("{:url('del')}",param,function(data){
        if(data.code == 200){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
        }
      });
    });
	
    return false;
  });
  
 
})
</script> 
